package jk.spider.core.throttle.impl;

import jk.spider.core.throttle.Throttle;
import jk.spider.core.throttle.ThrottleProvider;
import jk.spider.util.config.PropertySet;

import org.apache.log4j.Logger;

public class DistributeLoadThrottleProvider implements ThrottleProvider {
	private static final Logger log = Logger.getLogger(DistributeLoadThrottleProvider.class);
	public static final String INTERVAL = "interval";
    public static final int INTERVAL_DEFAULT = 1000;
    public static final int INTERVAL_MIN = 250;
    
	public Throttle createThrottle(PropertySet props) {
		int interval = props.getInteger(INTERVAL, INTERVAL_DEFAULT);

        if (interval < INTERVAL_MIN) {
            log.warn("Throttle interval < " + INTERVAL_MIN + " ms is dangereous - set to minimum allowed of " + INTERVAL_MIN + " ms");
            interval = INTERVAL_MIN;
        }

        log.debug("throttle interval set to " + interval + " ms.");

        return new DistributedLoadThrottleImpl(interval);
	}
}
